package com.lucaswangdev.biz.service.impl;

import com.lucaswangdev.biz.manager.UserManager;
import com.lucaswangdev.biz.request.user.UserCreateRequest;
import com.lucaswangdev.biz.request.user.UserDeleteRequest;
import com.lucaswangdev.biz.request.user.UserQueryRequest;
import com.lucaswangdev.biz.request.user.UserUpdateRequest;
import com.lucaswangdev.biz.response.common.PagingResponse;
import com.lucaswangdev.biz.service.UserService;
import com.lucaswangdev.constant.common.ErrorConstants;
import com.lucaswangdev.exception.BusinessException;
import com.lucaswangdev.model.convert.UserConverter;
import com.lucaswangdev.model.domain.UserParam;
import com.lucaswangdev.model.dto.UserDTO;
import com.lucaswangdev.util.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserManager userManager;

    @Override
    public Boolean create(UserCreateRequest userCreateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(userCreateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        UserDTO userDTO = UserConverter.INSTANCE.convertRequest2DTO(userCreateRequest);
        userManager.create(userDTO);
        return Boolean.TRUE;
    }

    @Override
    public PagingResponse<UserDTO> listPage(UserQueryRequest queryRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(queryRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        UserParam param = new UserParam();
        Integer currentPage;
        Integer pageSize;
        if (queryRequest.getCurrentPage() != null) {
            currentPage = queryRequest.getCurrentPage();
        } else {
            currentPage = 1;
        }
        if (queryRequest.getPageSize() != null) {
            pageSize = queryRequest.getPageSize();
        } else {
            pageSize = 20;
        }
        param.setCurrentPage(currentPage);
        param.setPageSize(pageSize);
        Long count = userManager.countByParam(param);
        if (count == null || count == 0) {
            return PagingResponse.build(Lists.newArrayList(), 0L, queryRequest.getCurrentPage(), queryRequest.getPageSize());
        }
        List<UserDTO> userDTOS = userManager.queryByParam(param);
        return PagingResponse.build(userDTOS, count, queryRequest.getCurrentPage(), queryRequest.getPageSize());
    }

    @Override
    public Boolean modified(UserUpdateRequest userUpdateRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(userUpdateRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        UserDTO updateUserDTO = UserConverter.INSTANCE.convertRequest2DTO(userUpdateRequest);
        userManager.update(updateUserDTO);
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteById(UserDeleteRequest userDeleteRequest) {
        // 参数校验
        String errorMsg = ValidatorUtils.validateAndReturnSingleErrorMsg(userDeleteRequest);
        if (StringUtils.isNotBlank(errorMsg)) {
            throw new BusinessException(ErrorConstants.PARAM_ERROR.getErrorCode(), errorMsg);
        }
        if(userDeleteRequest.getId() != null) {
            userManager.deleteById(userDeleteRequest.getId());
        }
        return Boolean.TRUE;
    }
}
